<template>
	<view class="add_shop">
		<u-toast ref="uToast"></u-toast>
		<uni-forms :value="formData" ref="form" :rules="rules">
			
			<uni-forms-item name="shopName" class="form_item">
				<view class="form_item_box">
					<text>商品名称</text>
					<input type="text" placeholder-style="margin-left:20rpx" v-model="formData.shopName"
							@input="binddata('shopName',$event.detail.value)" />
				</view>
			</uni-forms-item>
		
			<uni-forms-item  name="shopCode" class="form_item">
				<view class="form_item_box">
					<text>商品编号</text>
					<input type="text" placeholder-style="margin-left:20rpx" v-model="formData.shopCode"
							@input="binddata('shopCode',$event.detail.value)" />
				</view>
			</uni-forms-item>
			
			<uni-forms-item  name="shopPrice" class="form_item">
				<view class="form_item_box">
					<text>商品价格</text>
					<input type="text" placeholder-style="margin-left:20rpx" v-model="formData.shopPrice"
							@input="binddata('shopPrice',$event.detail.value)" />
				</view>
			</uni-forms-item>
		
			<!-- <uni-forms-item name="userTypeName" class="form_item">
				<view class="form_item_box">
					<text>商品价格</text>
					<picker @change="typePickerChange" :value="selectIndex" :range="typeArr" range-key="text">
						<input type="text" v-model="formData.userTypeName"
							@input="binddata('userTypeName',$event.detail.value)" disabled>
					</picker>
					<image class="row" src="../../static/images/row.png" mode=""></image>
				</view>
			</uni-forms-item> -->
		
			<uni-forms-item name="cover" class="form_item upload_form_item">
				<view class="form_item_box upload_box">
					<view class="upload_title">商品图片</view>
					<!-- <input type="text" v-model="formData.cover" @input="binddata('cover',$event.detail.value)"
						disabled>
					<image src="../../static/images/date.png" mode=""></image> -->
					<view class="">
						<u-upload
							:fileList="fileList1" 
							:maxCount="4"
							multiple 
							@afterRead="afterRead" 
							@delete="deletePic" >
						</u-upload>
					</view>
				</view>
			</uni-forms-item>
		
			<button @click="submitForm" class="form_btn">提交申请</button>
		</uni-forms>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				formData: {
					shopName:'',
					shopCode:'',
					shopPrice:'',
					cover: 'https://cdn.uviewui.com/uview/swiper/swiper3.png'
				},
				typeArr:[{
						text: '提货',
						value: '0'
					},
					{
						text: '配送',
						value: '1'
					}
				],
				selectIndex:0,
				rules: {
					shopName: {
						rules: [{
							required: true,
							errorMessage: '请填写商品名称',
						}]
					},
					shopCode: {
						rules: [{
							required: true,
							errorMessage: '请填写商品编号',
						}]
					},
					shopPrice: {
						rules: [{
							required: true,
							errorMessage: '请填写商品价格',
						}]
					},
					// cover: {
					// 	rules: [{
					// 		required: true,
					// 		errorMessage: '请选择预约时间',
					// 	}]
					// },
				},
				fileList1: [],
			}
		},
		methods:{
			submitForm(form) {
				console.log(this.$refs.form)
				this.$refs.form.validate().then(() => {
					console.log('添加',this.formData)
					uniCloud.callFunction({
						name: 'addShop',
						// 新增
						data: this.formData,				
						success:(res) => {
							console.log('成功了',res)
							if(res.result.res_code === 0){
								this.$refs.uToast.show({
									type: 'error',
									message: res.result.res_message,
								})
							}else{
								uni.switchTab({
									url:'../shopList/shopList',
									success: function(e) {
										let page = getCurrentPages().pop();
										if (page == undefined || page == null) return;
										page.onLoad();
									}
								})
							}
							
						},
						fail: (err) => {
							console.log('失败了',err)
						}
					})
				})
			},
			typePickerChange(e){
				console.log('预约类型',e)
			},
			// 删除图片
			deletePic(event) {
				console.log('删除', event)
				this.fileList1.splice(event.index, 1)
			},
			// 新增图片
			async afterRead(event) {
				console.log('新增', event)
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				this.fileList1 = lists
				// let fileListLen =this.fileList1.length
				// lists.map((item) => {
				// 	this.fileList1.push({
				// 		...item,
				// 		status: 'uploading',
				// 		message: '上传中'
				// 	})
				// })
				// console.log(2,lists)
				// console.log(3,this.fileList1)
				// let imageUrls = []
				// for (let i = 0; i < lists.length; i++) {
				// 	// const result = await this.uploadFilePromise(lists[i].url)
				// 	let item = this.fileList1[fileListLen]
				// 	this.fileList1.splice(fileListLen, 1, Object.assign(item, {
				// 		status: 'success',
				// 		message: '',
				// 		url: lists[i].url
				// 	}))
				// 	imageUrls.push(lists[i].url)
				// 	fileListLen++
				// }
				// console.log(4,imageUrls)
				
				uniCloud.callFunction({ //调用云端函数，把图片地址写入表
					name: 'addShop', //云函数名称
					data: { //提交给云端的数据
						cover: imageUrls,
						createTime: Date.now(),
						shopName:'测试图片',
						shopCode:'8600',
						shopPrice:'52.3',
					},
					success: (res) => {
						//console.log('数据插入成功')
						console.log(res)
						resolve(res)
					},
					fail: (err) => {
						console.log(err)
					},
					complete: () => {
									
					}
				})
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					// let a = uni.uploadFile({
					// 	url: 'http://192.168.2.21:7001/upload', // 仅为示例，非真实的接口地址
					// 	filePath: url,
					// 	name: 'file',
					// 	formData: {
					// 		user: 'test'
					// 	},
					// 	success: (res) => {
					// 		setTimeout(() => {
					// 			resolve(res.data.data)
					// 		}, 1000)
					// 	}
					// });
					uniCloud.uploadFile({
						filePath: url, //要上传的文件对象
						cloudPath: Date.now() + '.jpg', //保存在云端的文件名，这里以时间戳命名
						success(res) {
							console.log(3, res.fileID)
							// let imageUrl = res.fileID //云端返回的图片地址
							resolve(res.fileID)
						},
						fail(err) {
							console.log(err)
						},
						complete() {
							uni.hideLoading()
						}
					});
				})
			},
		}
	}
</script>
<style lang="scss" scoped>
	.add_shop {
		// width: 100%;
		height: 100%;
		margin: 40rpx 52rpx;

		.form_item {
			// width: 100%;
			// height: 68rpx;
			display: block;
			align-items: center;
			margin-bottom: 32rpx;
			font-size: 28rpx;
			// border: 2rpx solid #333333;

			.form_item_box {
				height: 100%;
				display: flex;
				align-items: center;

				text {
					flex: 1.5;
					margin-right: 32rpx;
				}

				input,
				picker {
					flex: 4;
					height: 68rpx;
					background: #EFF4F7;
					border-radius: 6px;
				}

				.row {
					position: absolute;
					right: 20rpx;
					height: 14rpx;
					width: 22rpx;
				}

				image {
					position: absolute;
					right: 20rpx;
					height: 30rpx;
					width: 30rpx;
				}
			}
			.upload_box{
				flex-direction: column;
				align-items:flex-start;
				.upload_title{
					// margin-top: 20rpx;
					margin-bottom: 20rpx;
				}
				
			}
		}
		.upload_form_item{
			margin-bottom: 0rpx;
			// height: var(--pHeight);
		}

		// 提交按钮
		.form_btn {
			display: block;
			border: none;
			// width: 630rpx;
			height: 84rpx;
			font-size: 32rpx;
			text-align: center;
			font-weight: 550;
			line-height: 84rpx;
			color: #FFFFFF;
			background-color: $uni-bg-color;
			border-radius: 20rpx;
			// box-shadow: 10px 10px 5px #888888;
			margin-bottom: 40rpx;
			// margin-top: 48rpx;

			&::after {
				border: none
			}
		}
	}
</style>